Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class cls_recordatorio_

    Implements ICloneable

#Region "Variables ..."
    Private int_idrecordatorio As Integer = 0
    Private int_tipo As Integer = 0
    Private dte_fecha As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private dte_hora As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private int_periodicidad As enu_tipoperiodicidad = 0
    Private int_tipoperiodicidad As Integer = 0

    Private col_Items As Collections.Generic.List(Of cls_recordatorio_) = Nothing

    Public Enum enu_tipoperiodicidad
        <Description("Ninguna")> Ninguna = 0
        <Description("Semanal")> Semanal = 1
        <Description("Mensual")> Mensual = 2
        <Description("Bimestral")> Bimestral = 3
        <Description("Semestral")> Semestral = 4
        <Description("Anual")> Anual = 5
    End Enum
#End Region

#Region "Propiedades ..."
    Property idrecordatorio() As Integer
        Get
            idrecordatorio = int_idrecordatorio
        End Get
        Set(ByVal value As Integer)
            int_idrecordatorio = value
        End Set
    End Property
    Property tipo() As Integer
        Get
            tipo = int_tipo
        End Get
        Set(ByVal value As Integer)
            int_tipo = value
        End Set
    End Property
    Property fecha() As DateTime
        Get
            fecha = dte_fecha
        End Get
        Set(ByVal value As DateTime)
            dte_fecha = value
        End Set
    End Property
    Property hora() As DateTime
        Get
            hora = dte_hora
        End Get
        Set(ByVal value As DateTime)
            dte_hora = value
        End Set
    End Property
    Property periodicidad() As Integer
        Get
            periodicidad = int_periodicidad
        End Get
        Set(ByVal value As Integer)
            int_periodicidad = value
        End Set
    End Property
    Property tipoperiodicidad() As enu_tipoperiodicidad
        Get
            tipoperiodicidad = int_tipoperiodicidad
        End Get
        Set(ByVal value As enu_tipoperiodicidad)
            int_tipoperiodicidad = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_recordatorio_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_recordatorio_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_recordatorio_)
        While obj_Reader.Read()
            Dim Elemento As New cls_recordatorio_
            Elemento.int_idrecordatorio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
                Elemento.int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
                Elemento.dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hora"))) = False Then
                Elemento.dte_hora = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hora")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("periodicidad"))) = False Then
                Elemento.int_periodicidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("periodicidad")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoperiodicidad"))) = False Then
                Elemento.int_tipoperiodicidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoperiodicidad")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idrecordatorio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
            int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
            dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hora"))) = False Then
            dte_hora = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hora")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("periodicidad"))) = False Then
            int_periodicidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("periodicidad")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoperiodicidad"))) = False Then
            int_tipoperiodicidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoperiodicidad")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__Nuevo"
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@hora", SqlDbType.DateTime).Value = dte_hora
                .Parameters.Add("@periodicidad", SqlDbType.Int).Value = int_periodicidad
                .Parameters.Add("@tipoperiodicidad", SqlDbType.Int).Value = int_tipoperiodicidad
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__Modificar"
                .Parameters.Add("@idrecordatorio", SqlDbType.Int).Value = int_idrecordatorio
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@hora", SqlDbType.DateTime).Value = dte_hora
                .Parameters.Add("@periodicidad", SqlDbType.Int).Value = int_periodicidad
                .Parameters.Add("@tipoperiodicidad", SqlDbType.Int).Value = int_tipoperiodicidad
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idrecordatorio As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__Eliminar"
                .Parameters.Add("@idrecordatorio", SqlDbType.Int).Value = idrecordatorio
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idrecordatorio As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__Buscar"
                .Parameters.Add("@idrecordatorio", SqlDbType.Int).Value = idrecordatorio
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "recordatorio__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de recordatorio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_recordatorio_Clon As New cls_recordatorio_
        obj_recordatorio_Clon.int_idrecordatorio = Me.int_idrecordatorio
        obj_recordatorio_Clon.int_tipo = Me.int_tipo
        obj_recordatorio_Clon.dte_fecha = Me.dte_fecha
        obj_recordatorio_Clon.dte_hora = Me.dte_hora
        obj_recordatorio_Clon.int_periodicidad = Me.int_periodicidad
        obj_recordatorio_Clon.int_tipoperiodicidad = Me.int_tipoperiodicidad
        obj_recordatorio_Clon.col_Items = Me.col_Items
        Return obj_recordatorio_Clon
    End Function

End Class
